"""
又拍云工具包
"""
from io import BytesIO
from upyun import UpYun, ED_AUTO
from application.config.ImageUploadConfig import UpyunConfig

# 创建又拍云客户端
upyun_client: UpYun = UpYun(
    service=UpyunConfig.service,
    username=UpyunConfig.username,
    password=UpyunConfig.password
)


def upload_image(filename: str, file_bytes: BytesIO) -> dict:
    """
    上传图片
    :param filename: 文件名称
    :param file_bytes: 文件字节流
    :return: {
        "unique_id": image_id,
        "url": image_url
    }
    """
    # 设置上传域名
    upyun_client.up_rest.endpoint = ED_AUTO
    save_dir: str = "/sweet-space/"
    filename = save_dir + filename
    upyun_client.put(key=filename, value=file_bytes.getvalue())
    return {
        "id": filename,
        "url": "http://" + UpyunConfig.domain + filename
    }


def delete_image(filename: str) -> None:
    """
    删除图片
    :param filename: OSS文件路径
    :return:
    """
    upyun_client.delete(key=filename)
